﻿using ChurchDiary.Common;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.DAL;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.Methods
{
    public class DAL_Promotion : DataAccess, IPromotion
    {
        public string InsertContactUs(EntityContactUs ContactUs)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_ContactUs]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, eMode.INS);
                    SQLServer.AddInParameter(dbCommand, "@ContactUsId", DbType.Guid, ContactUs.ContactUsId);
                    SQLServer.AddInParameter(dbCommand, "@Name", DbType.String, ContactUs.Name);
                    SQLServer.AddInParameter(dbCommand, "@Email", DbType.String, ContactUs.Email);
                    SQLServer.AddInParameter(dbCommand, "@Phone", DbType.String, ContactUs.Phone);
                    SQLServer.AddInParameter(dbCommand, "@Message", DbType.String, ContactUs.Message);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = SQLServer.ExecuteScalar(dbCommand).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }

        public string UpdateContactUs(EntityContactUs ContactUs)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_ContactUs]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, eMode.UPD);
                    SQLServer.AddInParameter(dbCommand, "@ContactUsId", DbType.Guid, ContactUs.ContactUsId);
                    SQLServer.AddInParameter(dbCommand, "@Name", DbType.String, ContactUs.Name);
                    SQLServer.AddInParameter(dbCommand, "@Email", DbType.String, ContactUs.Email);
                    SQLServer.AddInParameter(dbCommand, "@Phone", DbType.String, ContactUs.Phone);
                    SQLServer.AddInParameter(dbCommand, "@Message", DbType.String, ContactUs.Message);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = SQLServer.ExecuteScalar(dbCommand).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }

        public string DeleteContactUs(Guid ContactUsId)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_ContactUs]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, eMode.DEL);
                    SQLServer.AddInParameter(dbCommand, "@ContactUsId", DbType.Guid, ContactUsId);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = SQLServer.ExecuteScalar(dbCommand).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }

        public List<EntityContactUs> GetContactus(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            List<EntityContactUs> ContactUs = new List<EntityContactUs>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_ContactUs]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, eMode.LIST);
                    SQLServer.AddInParameter(dbCommand, "@PageNumber", DbType.Int32, PageNo);
                    SQLServer.AddInParameter(dbCommand, "@SearchKey", DbType.String, SearchKey);
                    SQLServer.AddInParameter(dbCommand, "@Sort", DbType.String, Sort);
                    SQLServer.AddInParameter(dbCommand, "@SortMode", DbType.String, SortMode);
                    SQLServer.AddInParameter(dbCommand, "@RowsPerPage", DbType.Int32, PerPage);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return ContactUs;
                            }
                            while (OReader.Read())
                            {
                                EntityContactUs Contact = new EntityContactUs();
                                Contact.ContactUsId = (Guid)OReader["ContactUsId"];
                                Contact.Name = OReader["Name"].ToString();
                                Contact.Email = OReader["Email"].ToString();
                                Contact.Message = OReader["Message"].ToString();
                                Contact.Phone = OReader["Phone"].ToString();
                                Contact.TotalRecords = (int)OReader["TotalRecords"];
                                ContactUs.Add(Contact);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return ContactUs;
        }

        public EntityContactUs GetContactUs(Guid ContactUsId)
        {
            EntityContactUs ContactUs = new EntityContactUs();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Language]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, eMode.ONE);
                    SQLServer.AddInParameter(dbCommand, "@ContactUsId", DbType.Guid, ContactUsId);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return ContactUs;
                            }
                            while (OReader.Read())
                            {
                                ContactUs.ContactUsId = (Guid)OReader["ContactUsId"];
                                ContactUs.Name = OReader["Name"].ToString();
                                ContactUs.Email = OReader["Email"].ToString();
                                ContactUs.Message = OReader["Message"].ToString();
                                ContactUs.Phone = OReader["Phone"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return ContactUs;
        }
    }
}
